/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Service.GestioneDB;
import Service.Previsione;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * azioni da implementare:
 * recupero previsione, aggiornamento csv presente nel dispositivo android.
 * @author Filippo
 */
@WebServlet(name = "AndroidServlet", urlPatterns = {"/AndroidServlet"}, initParams = {
    @WebInitParam(name = "ServerAddress", value = "localhost")})
public class AndroidServletDataRec extends HttpServlet {
    private String descrizione ="Servlet for weather forecasts.";
    private String versione="1.0";
    private GestioneDB dbManager;
    /**
     * 
     * @param citta
     * @return 
     */
    private String getPrevisioniCitta(String citta){
        dbManager= GestioneDB.getInstance();
        Previsione p;
        String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
                    +"<information city=\""+citta+"\" id=\"";
        //System.out.println(xml);
        xml += dbManager.getIdCitta(citta)+"\" >\n";
        
        for(int i=0;i<7; i++){
           p= dbManager.previsioneCitta(citta, i);
           if (p!=null){
                xml+="<day value=\""+p.stampaDataPrevisione()+"\">\n";
                xml+="\t<tmin>"+p.gettMin()+"</tmin>\n"+
                        "\t<tmax>"+p.gettMax()+"</tmax>\n"+
                        "\t<description>"+p.getPrevisione()+"</description>\n"+
                        "\t<precipitation>"+p.getPercentualePrecipitazioni()+"</precipitation>\n"+
                        "\t<wind>"+p.getVentoKmh()+"</wind>\n"+
                        "\t<zeroLevel>"+p.getQuotaZero()+"</zeroLevel>\n"+
                        "\t<pressure>"+p.getPressione()+"</pressure>\n";

                xml+="</day>\n";
                }
           }
        xml+="</information>\n";
        System.out.println(xml);
        return xml;
    }
    
    private void forward(HttpServletRequest request, HttpServletResponse response, String page) throws ServletException,IOException {
        ServletContext sc = getServletContext();
        RequestDispatcher rd = sc.getRequestDispatcher(page);
        rd.forward(request, response);
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            String operation = request.getParameter("operation");
            if (operation==null){
                forward(request,response,"/AndroidServlet?operation=getInfo");
                }
            if (operation.equalsIgnoreCase("getInfo")){
            out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            out.println("<information>");
            out.println("<name>Servlet AndroidServlet</name>");
            out.println("<version>"+versione+"</version>");
            out.println("<description>"+descrizione+"</description>");
            out.println("<context>Servlet AndroidServlet at " + request.getContextPath() + "</context>");
            out.println("</information>");
            }
            if (operation.equalsIgnoreCase("getPrevisioniCitta")){
                String citta = request.getParameter("citta");
                if (citta==null)
                    citta="null";
                String xml = getPrevisioniCitta(citta);
                out.println(xml);
            }
            if (operation.equalsIgnoreCase("getCitta")){
                dbManager= GestioneDB.getInstance();
                String[] citta= dbManager.getCitta();
                out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                out.println("<information>");
                for(int i=0; i<citta.length; i++)
                    out.print("<item>"+citta[i]+"</item>\n");
                
                out.println("</information>");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Servlet providing the xml content for mobile application";
    }// </editor-fold>
}
